import re

def join_section_name(section_name):
    section_name = section_name.lower()
    return "-".join(section_name.split())


def section_formatter(lines):
    accum = []
    for line in lines:
        m = re.search(r'\\section\{(.*)\}\W*', line)

        if(m != None):
            print m.group(1)
            section_name = join_section_name(m.group(1))
            line = "\\section{%s} \\label{section.%s}" % (m.group(1), section_name)

        accum.append(line)

    return accum

def subsection_formatter(lines):
    accum = []
    for line in lines:
        m = re.search(r'\\subsection\{(.*)\}\W*', line)

        if(m != None):
            print m.group(1)
            section_name = join_section_name(m.group(1))
            line = "\\subsection{%s} \\label{subsection.%s}" % (m.group(1), section_name)

        accum.append(line)

    return accum

def subsubsection_formatter(lines):
    accum = []
    for line in lines:
        m = re.search(r'\\subsubsection\{(.*)\}\W*', line)

        if(m != None):
            print m.group(1)
            section_name = join_section_name(m.group(1))
            line = "\\subsubsection{%s} \\label{subsubsection.%s}" % (m.group(1), section_name)

        accum.append(line)

    return accum

def texttt_replacer(lines):
    accum = []
    for line in lines:
        accum.append(re.sub(r'\\texttt\{([^\}]+)\}', '\\lstinline\'\\1\'', line))
    return accum
        
races = open('monitors.tex', 'r')
lines = texttt_replacer(races.readlines())

races_out = open('monitors-new.tex', 'w')
races_out.writelines(lines)
